package com.ruoyi.project.jiaowu.plan.controller;


import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaowu.plan.domain.AddPlanListParam;
import com.ruoyi.project.jiaowu.plan.domain.EditTbPlanMain;
import com.ruoyi.project.jiaowu.plan.domain.TbPlabAddListDto;
import com.ruoyi.project.jiaowu.plan.domain.TbPlanMain;
import com.ruoyi.project.jiaowu.plan.service.ITbPlanMainService;
import com.ruoyi.project.jiaowu.totalcoursemain.domain.TbTotalCourseMain;
import com.ruoyi.project.jiaowu.totalcoursemain.mapper.TbTotalCourseMainMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【教务：培养计划】Controller
 * 
 * @author ruoyi
 * @date 2019-10-23
 */
@Controller
@RequestMapping("/website/trainplan")
public class TbPlanMainController extends BaseController {


    @Autowired
    private TbTotalCourseMainMapper tbTotalCourseMainMapper;

    private String prefix = "system/jiaowuplan";

//    //todo 动态加载 总课程
//    @ModelAttribute("list")
//    public List<TbTotalCourseMain> courseList(){
//        List<TbTotalCourseMain> list = tbTotalCourseMainMapper.selectTbTotalCourseMainList(null);
//        return list;
//    }


    @Autowired
    private ITbPlanMainService tbPlanMainService;

    //

    @RequiresPermissions("website:trainplan:view")
    @GetMapping()
    public String main()
    {
        return prefix + "/main";
    }

    /**
     * 查询【教务培养计划】列表
     */
    @RequiresPermissions("website:trainplan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbPlanMain tbPlanMain)
    {
        startPage();
        List<TbPlanMain> list = tbPlanMainService.selectTbPlanMainList(tbPlanMain);
        //System.out.println("你好："+list);
        return getDataTable(list);
    }

    /**
     * 导出【教务培养计划】列表
     */
    @RequiresPermissions("website:trainplan:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbPlanMain tbPlanMain)
    {
        List<TbPlanMain> list = tbPlanMainService.selectTbPlanMainList(tbPlanMain);
        ExcelUtil<TbPlanMain> util = new ExcelUtil<TbPlanMain>(TbPlanMain.class);
        return util.exportExcel(list, "main");
    }

    /**
     * 新增【教务培养计划】
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        return prefix + "/add";
    }

    @PostMapping("/addList")
    @ResponseBody
    public TableDataInfo addList(Long majorId)
    {
        List<TbPlabAddListDto> list = tbPlanMainService.selectAddPlanList(majorId);
        return getDataTable(list);
    }


    /**
     * 获取修改页面所有的课程
     * @param majorId  专业id
     * @param id       培养计划id
     * @return
     */
    @PostMapping("/arrList/{id}")
    @ResponseBody
    public TableDataInfo listCourse(Long majorId,Integer id){
        /*System.err.println(id);
        System.err.println(majorId);*/
        //根据专业id获取到专业下的课程信息
        List<EditTbPlanMain> list = tbPlanMainService.selectEditPlanList(majorId);
        //获取到该培养计划下的所有的课程id
        List<String> coursIds=tbPlanMainService.getAllCourseId(id);
        for(String ids:coursIds){
            for(int i=0;i<list.size();i++){
                if(ids.equals(list.get(i).getId().toString())){
                    list.get(i).setCode(1);
                    //根据id查询出其余状态
                    Map<String,Object> map=new HashMap<String,Object>();
                    map.put("courseId",ids);
                    map.put("planId",id);
                    EditTbPlanMain ed=tbPlanMainService.selectEditplan(map);
                    list.get(i).setChooseCourseType(ed.getChooseCourseType());
                    list.get(i).setCourseType(ed.getCourseType());
                    list.get(i).setTerm(ed.getTerm());
                    list.get(i).setAssessWay(ed.getAssessWay());
                }
            }
        }
        //System.out.println(list);
        return getDataTable(list);
    }


    /**
     * 修改保存
     * @param tbPlanMain
     * @return
     */
    @RequiresPermissions("website:trainplan:edit")
    @PostMapping(value="/editAddPlanList")
    @ResponseBody
    public AjaxResult editAddPlanList(@RequestBody List<AddPlanListParam> tbPlanMain){
        //删除该计划及其所有已选课程
        Long planId=tbPlanMain.get(0).getPlanId();
        tbPlanMainService.deletePlan(planId);
        return tbPlanMainService.insertTbPlanMainList(tbPlanMain);
    }





    /**
     * 新增保存【教务培养计划】
     */
    @RequiresPermissions("website:trainplan:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbPlanMain tbPlanMain)
    {
        return toAjax(tbPlanMainService.insertTbPlanMain(tbPlanMain));
    }

    /**
     * 新增保存【教务培养计划】
     */
    @Log(title = "【添加培养计划】", businessType = BusinessType.INSERT)
    @RequiresPermissions("website:trainplan:add")
    @PostMapping(value="/addPlanList")
    @ResponseBody
    public AjaxResult addPlanList(@RequestBody List<AddPlanListParam> tbPlanMain)
    {
        //System.out.println(tbPlanMain);
        return tbPlanMainService.insertTbPlanMainList(tbPlanMain);
    }


    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        //根据培养计划id获取到指定培养计划，跳转到修改页面
        TbPlanMain tbPlanMain = tbPlanMainService.selectTbPlanMainById(id);
        //mmap.put("tbPlanMain", tbPlanMain);
        /*List<TbTotalCourseMain> list = tbTotalCourseMainMapper.selectTbTotalCourseMainList(null);
        mmap.addAttribute("list",list);*/
        //System.err.println(tbPlanMain);
        mmap.addAttribute("tbPlanMain",tbPlanMain);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("website:trainplan:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbPlanMain tbPlanMain)
    {
        return toAjax(tbPlanMainService.updateTbPlanMain(tbPlanMain));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("website:trainplan:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //System.out.println("controller:"+ids);
        return toAjax(tbPlanMainService.deleteTbPlanMainByIds(ids));
    }


    /**
     * 发布培养计划
     * @param id
     * @return
     */
    @RequiresPermissions("website:trainplan:fabu")
    @PostMapping("/fabu")
    @ResponseBody
    public AjaxResult faBu(Integer id){
        System.out.println("你好"+id);
        return toAjax(tbPlanMainService.fabu(id));
    }

    /**
     * 取消发布培养计划
     * @param id
     * @return
     */
    @RequiresPermissions("website:trainplan:quxiaofabu")
    @PostMapping("/quxiaofabu")
    @ResponseBody
    public AjaxResult quxiaoFaBu(Integer id){
        return toAjax(tbPlanMainService.quxiaoFabu(id));
    }
}
